package com.rbac.mapper.test;

import com.rbac.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper2 {
    @Select("select * from role")
    List<Role> getList(Integer pageNum, Integer pageSize);

    @Select("select * from role where id = #{id}")
    Role getRoleById(Integer id);

    @Delete("delete from role where id = #{id}")
    void delete(Integer id);

    @Insert("insert into role (name, description) values (#{name}, #{description}) ")
    void save(Role role);

    @Update("update role set name = #{name}, description = #{description} where id =#{id}")
    void update(Role role);

    @Select("select * from role")
    List<Role> getRoleAllById();

    //删除中间表
    @Delete("delete from user_role where role_id = #{roleId}")
    void deleteRoleUser(Integer roleId);
    @Delete("delete from role_permission where role_id = #{roleId}")
    void deletePermissionRole(Integer roleId);

    //查询中间表
    @Select("select user_id from user_role where role_id = #{roleId}")
    List<Integer> getRoleUserData(Integer roleId);
    @Select("select permission_id from role_permission where role_id = #{roleId}")
    List<Integer> getRolePermissionData(Integer roleId);

    @Insert("insert into user_role (user_id, role_id) values (#{userId}, #{roleId})")
    void saveUserRole(@Param("userId")Integer userId, @Param("roleId")Integer roleId);
    @Insert("insert into role_permission (permission_id, role_id) values (#{permissionId}, #{roleId})")
    void saveRolePermission(@Param("permissionId")Integer permissionId, @Param("roleId")Integer roleId);

    //在角色用户中间表查询对应角色对应的用户
    @Select("select e.id from employee e\n" +
            "    left join user_role ur on e.id = ur.user_id\n" +
            "    left join role r on ur.role_id = r.id\n" +
            "    where r.id = #{roleId};")
    List<Integer> getUserRole(Integer roleId);

    @Select("select p.id from permission p\n" +
            "    left join role_permission rp on p.id = rp.permission_id\n" +
            "    left join role r on rp.role_id = r.id\n" +
            "    where r.id = #{roleId};")
    List<Integer> getRolePermission(Integer roleId);


    //TODO user_role相关设置，包括更新，新增（动态），等
}
